<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--修改标记模态框-->
<div class="modal fade" id="modal-update">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">库存划项目标记修改</h4>
            </div>
            <div class="modal-body">
                <form id="editMarkForm" class="form-horizontal" role="form">
                    <input type="hidden" class="form-control" id="ipmId" name="ipmId">
                    <input type="hidden" id="matSpecEdit">
                    <input type="hidden" id="matLengthEdit">
                    <input type="hidden" id="matWidthEdit">
                    <input type="hidden" id="matHeightEdit">
                    <input type="hidden" id="formulaTypeEdit">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">原料描述</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="materialDescEdit"
                                   readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">计划物料描述</label>
                        <div class="col-sm-10">
                            <input type="text" autocomplete="off" class="form-control" name="ipmNameDescExcel"
                                   id="ipmNameDescExcelEdit"
                                   placeholder="请输入计划物料描述">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">新项目编号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="ipmInProjectCode" id="ipmInProjectCodeEdit"
                                   maxlength="8"
                                   placeholder="请输入新项目编号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">新项目名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="ipmInProjectName" id="ipmInProjectNameEdit"
                                   placeholder="请输入新项目名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">可用数量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="ityAvaNumEdit" readonly="readonly">
                        </div>
                        <label class="col-sm-2 control-label no-padding-right">可用重量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="ityAvaWeightEdit" readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">标记数量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="ipmNumEdit" name="ipmNum"
                                   autocomplete="off" placeholder="请输入标记数量">
                        </div>
                        <label class="col-sm-2 control-label no-padding-right">标记重量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="ipmWeightEdit" name="ipmWeight"
                                   autocomplete="off" placeholder="请输入标记重量">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">备注</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="ipmRemarkEdit" name="ipmRemark"
                                   placeholder="请输入备注">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-editMark">确定</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>
        库存划项目标记记录
    </h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" action="/material/ityToPro/exportExcel">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" name="ipmInProjectCode" class="form-control" id="ipmInProjectCode"
                                           placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" name="ipmInProjectName" class="form-control"
                                           placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label>原料类型</label>
                                    <input type="text" name="matTypeName"
                                           class="form-control"
                                           placeholder="请输入原料类型">
                                </div>
                                <div class="form-group">
                                    <label>原料材质</label>
                                    <input type="text" name="matQualityName" class="form-control"
                                           placeholder="请输入原料材质">
                                </div>
                                <div class="form-group">
                                    <label>原料规格</label>
                                    <input type="text" name="matSpecName" class="form-control"
                                           placeholder="请输入原料规格">
                                </div>
                                <div class="form-group">
                                    <label>宽度</label>
                                    <input type="number" step="0.1" name="matWidth" class="form-control"
                                           placeholder="请输入原料宽度">
                                </div>
                                <div class="form-group">
                                    <label>长度</label>
                                    <input type="number" step="0.1" name="matLength" class="form-control"
                                           placeholder="请输入原料长度">
                                </div>
                                <div class="form-group">
                                    <label>米重/厚度</label>
                                    <input type="number" step="0.1" name="matHeight" class="form-control"
                                           placeholder="请输入原料米重/厚度">
                                </div>
                                <div class="form-group">
                                    <label>标记人员</label>
                                    <input type="text" name="ipmProposer"
                                           class="form-control"
                                           placeholder="请输入标记人员" th:value="${userName}">
                                </div>
                                <div class="form-group">
                                    <label>状态</label>
                                    <select name="ipmAppStatus" class="form-control">
                                        <option value="1" selected="selected">未提交</option>
                                        <option value="2">已提交</option>
                                        <option value="3">已执行</option>
                                        <option value="4">被驳回</option>
                                        <option value="">全部</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>标记日期</label>
                                    <input type="text" class="form-control datepicker" id="startTimeQuery"
                                           name="startTimeQuery"
                                           readonly="readonly"/>
                                    <span>至</span>
                                    <input type="text" class="form-control datepicker" id="endTimeQuery"
                                           name="endTimeQuery"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn_searchMark">搜索</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="row" id="toolbar1">
                    <div class="form-inline">
                        <button shiro:hasPermission="matinv:matwarpro:edit" type="button"
                                class="btn btn-success"
                                id="btn-togetherCommit">
                            批量提交
                        </button>
                        <a shiro:hasPermission="matinv:matwarpro:edit" th:href="@{/material/ityToPro/createPage}"
                           class="btn btn-info" id="btn-add-flag">新增标记</a>
                        <a shiro:hasPermission="matinv:matwarproexport:add" th:href="@{/material/ityToPro/import}"
                           class="btn btn-info">批量标记导入</a>
                        <button type="button" id="btn_exportExcel"
                                class="btn btn-primary">
                            导出
                        </button>
                    </div>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    /**加载表格*/
    var table1;

    /** 更改状态 */
    function updateStatus(id, status) {
        js.modal.confirm("确定" + (status == 0 ? "删除" : "提交") + "库存划项目标记吗？", function (result) {
            js.post({
                url: ctx + "material/ityToPro/updateStatus",
                data: {
                    ipmId: id,
                    ipmAppStatus: status
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table1);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            })
        })
    }

    /** 打开单条修改模态框 */
    function editMark(id, d) {
        $("#ipmId").val(id);
        $("#materialDescEdit").val(d.matNameDesc);
        $("#ipmNameDescExcelEdit").val(d.ipmNameDescExcel);
        $("#ipmInProjectCodeEdit").val(d.ipmInProjectCode);
        $("#ipmInProjectNameEdit").val(d.ipmInProjectName);
        $("#ipmNumEdit").val(d.ipmNum);
        $("#ipmWeightEdit").val(d.ipmWeight);
        $("#ipmRemarkEdit").val(d.ipmRemark);

        $("#ityAvaNumEdit").val((d.ityAvaNum + d.ipmNum).toFixed(1));
        $("#ityAvaWeightEdit").val((d.ityAvaWeight + d.ipmWeight).toFixed(2));

        $("#matSpecEdit").val(d.matSpecName);
        $("#matLengthEdit").val(d.matLength);
        $("#matWidthEdit").val(d.matWidth);
        $("#matHeightEdit").val(d.matHeight);
        $("#formulaTypeEdit").val(d.matFormulaType);

        js.modal.open("modal-update");
    }

    $(function () {

        /** 加载列表 */
        table1 = js.table.init({
            url: ctx + "material/ityToPro/markList",
            pageSize: 300,
            toolbar: "#toolbar1",
            columns: [
                {
                    field: "checked",
                    title: '全选',
                    checkbox: true,
                    formatter: function (value, row, index) {
                        if (row.ipmAppStatus === 1) {
                            return {
                                disabled: false
                            }
                        } else {
                            return {
                                disabled: true
                            }
                        }
                    }
                },
                {title: '物料描述', field: 'matNameDesc'},
                {title: '项目编号', field: 'ipmInProjectCode'},
                {title: '项目名称', field: 'ipmInProjectName'},
                {title: '计划物料', field: 'ipmNameDescExcel'},
                {title: '申请重量', field: 'ipmWeightExcel'},
                {title: '标记数量', field: 'ipmNum'},
                {title: '标记重量', field: 'ipmWeight'},
                {
                    title: '状态', field: 'ipmAppStatus', width: '80',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return '<span class="badge bg-gray">未提交</span> ';
                        } else if (value === 2) {
                            return '<span class="badge bg-yellow">已提交</span> ';
                        } else if (value === 3) {
                            return '<span class="badge bg-green">已执行</span> ';
                        } else if (value === 4) {
                            return '<span class="badge bg-red">被驳回</span> ';
                        }
                    }
                },
                {title: '标记人员', field: 'ipmProposer', width: '90'},
                {title: '标记时间', field: 'ipmDate'},
                {title: '库位', field: 'whiName'},
                {
                    title: '操作', width: '140',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.ipmAppStatus === 1 || row.ipmAppStatus === 4) {
                            if (hasP("matinv:matwarpro:edit") && (row.ipmAccount === sessionUser.account || sessionUser.account === "admin")) {
                                actions.push("<a class='btn btn-warning btn-xs'  href='javascript:void(0);' onclick='editMark(" + row.ipmId + "," + JSON.stringify(row) + ")'>修改</a>");
                            }
                            if (hasP("matinv:matwarpro:del") && (row.ipmAccount === sessionUser.account || sessionUser.account === "admin")) {
                                actions.push('<a style="margin-right: 5px" class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="updateStatus(' + row.ipmId + ',0)"> 删除</a>');
                            }
                            if (hasP("matinv:matwarpro:edit") && (row.ipmAccount === sessionUser.account || sessionUser.account === "admin")) {
                                actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0);" onclick="updateStatus(' + row.ipmId + ',2)" >提交</a>');
                            }
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 表格搜索 */
        $("#btn_searchMark").click(function () {
            js.table.search(table1);
        });

        /** 批量提交 */
        $("#btn-togetherCommit").click(function () {

            var rows = js.table.selectRows("bootstrap-table");

            // 如果没有选择，显示提示信息
            if (rows.length == 0) {
                js.modal.warning("请至少选择一条明细");
                return false;
            } else {
                var arr = js.table.selectColumns(table1, "ipmId");
                js.post({
                    url: ctx + "material/ityToPro/togetherCommit",
                    contentType: "application/json",
                    data: JSON.stringify(arr),
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            js.table.refresh(table1);
                        } else {
                            js.modal.warning(result.msg);
                            js.table.refresh(table1);
                        }
                        js.modal.closeLoading();

                    }
                })
            }
        });
        /**标记数量变更 联动标记重量*/
        $("#ipmNumEdit").change(function () {
            var spec = $("#matSpecEdit").val();
            var length = $("#matLengthEdit").val();
            var width = $("#matWidthEdit").val();
            var height = $("#matHeightEdit").val();
            var needQty = $("#ipmNumEdit").val();
            var formulaType = $("#formulaTypeEdit").val();
            var totalWeight = returnMaterialWeight(spec, length, width, height, needQty, formulaType);
            $("#ipmWeightEdit").val(totalWeight);
        });
        /**关闭单条修改模态框*/
        js.modal.hideEvent("modal-update", function () {
            js.reset("editMarkForm");
            js.validate.reset("editMarkForm");
        });
        /** 单条标记修改验证 */
        js.validate.init("editMarkForm", {
            fields: {
                // ipmNameDescExcel: {validators: {notEmpty: {message: '请输入计划物料描述'}}},
                ipmInProjectCode: {
                    validators: {
                        threshold: 6, //有6字符以上才发送ajax请求，（input中输入一个字符，插件会向服务器发送一次，设置限制，6字符以上才开始）
                        notEmpty: {
                            message: '请输入新项目编号'
                        },
                        // 发送ajax请求到后台查询，返回 valid：true或者false
                        callback: {
                            message: "项目号不存在",
                            callback: function (value, validator) {
                                var flag = false;
                                $.ajax({
                                    url: ctx + 'material/changeOrder/codeExist', // 请求路径
                                    type: 'POST',
                                    async: false,	// 同步请求，必须是false
                                    data: {
                                        ipmInProjectCode: $('#ipmInProjectCodeEdit').val() // 请求参数
                                    },
                                    success: function (res) {
                                        flag = $.parseJSON(res).valid;
                                    }
                                });
                                return flag;
                            }
                        }
                    }
                },
                ipmInProjectName: {validators: {notEmpty: {message: '请输入新项目名称'}}},
            }
        });
        /**修改单条库存划项目标记*/
        $("#btn-editMark").click(function () {
            var ipmInProjectCode = $("#ipmInProjectCodeEdit").val();
            if (ipmInProjectCode != null && ipmInProjectCode != "" && ipmInProjectCode.length != 8) {
                js.modal.warning("新项目编号需要8位字符串！例如：2018X001");
                $("#ipmInProjectCodeEdit").focus();
                return;
            }
            var ipmWeight = $("#ipmWeightEdit").val();
            if (ipmWeight == null || ipmWeight == "") {
                js.modal.warning("请输入标记重量！");
                $("#ipmWeightEdit").focus();
                return false;
            }
            var ipmNum = $("#ipmNumEdit").val();
            var ityAvaNum = $("#ityAvaNumEdit").val();
            var ityAvaWeight = $("#ityAvaWeightEdit").val();
            if (ipmNum != null && ipmNum != "" && ityAvaNum != null && ityAvaNum != "" && parseFloat(ipmNum) > parseFloat(ityAvaNum)) {

                js.modal.warning("标记数量大于可用数量！");
                $("#ipmNumEdit").focus();
                return false;
            }
            if (ipmWeight != null && ipmWeight != "" && ityAvaWeight != null && ityAvaWeight != "" && parseFloat(ipmWeight) > parseFloat(ityAvaWeight)) {

                js.modal.warning("标记重量大于可用重量！");
                $("#ipmWeightEdit").focus();
                return false;
            }
            js.validSubmit({
                formId: "editMarkForm",
                url: ctx + "material/ityToPro/edit",
                data: new FormData($("#editMarkForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.hide("modal-update");
                        js.modal.success("操作成功");
                        js.table.refresh(table1);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })
        /** 项目编号 焦点离开 自动取得 名称 */
        $("#ipmInProjectCodeEdit").blur(function () {
            js.get({
                url: ctx + "material/changeOrder/findProject",
                data: {proCode: $("#ipmInProjectCodeEdit").val()},
                success: function (result) {
                    if (result.data == null) {
                        $("#ipmInProjectNameEdit").val("");
                    } else {
                        $("#ipmInProjectNameEdit").val(result.data.proName);
                    }
                }
            });
        });
        /**
         * 导出Excel
         */
        $("#btn_exportExcel").click(function () {

            // 判断表单数据是否为空,为空不允许导出
            var isAllEmpty = true;
            var t = $('#queryForm').serializeArray();
            $.each(t, function (i, item) {
                if (item['value'] != '') {
                    isAllEmpty = false;
                }
            })
            if (isAllEmpty) {
                $("#ipmInProjectCode").focus();
                js.modal.warning("请填写导出筛选条件！");
                return false;
            }
            // 判断如果起始日期不为空，则终止日期必填
            if ($("#startTimeQuery").val() != "" && $("#endTimeQuery").val() == "") {
                $("#endTimeQuery").focus();
                js.modal.warning("请选择导出终止日期！");
                return false;
            }
            $("#queryForm").submit();
        });
    })
</script>
</body>
</html>